#
# coding: utf-8
#
# Retro-X/config:  v1.50 (Primary Release).
#

#import  xbmc, xbmcgui
import  os,     sys,    string,     threading,      time,   traceback
from    re          import findall      as reFindall, compile as reCompile  , sub as reSub, DOTALL as reDotAll, search as reSearch
from    copy        import deepcopy

import core

## Constants (NO NEED TO CHANGE)
VERSION         =   '1.50'                                  # version of Retro-X.
HOME_DIR        =   os.getcwd().replace(";","")+"\\"        # get homedirectory for the script
SRCXML          =   'Q:\\UserData\\sources.xml'             # Path to xbmc sources.xml
CUT_FILE        =   "launch.cut"                            # Name of file to call emulator + game               


def GetEmus()   :
    try :
        ## Open the Sources.xml file
        f       =   open(SRCXML,'r')
        XmlDta  =   f.read()
        f.close()

        ## Get the Emulators record
        Exp     =   reCompile('<bookmark>.*?<name>Emulators</name>(.*?)</bookmark>',reDotAll)        
        Tar     =   reFindall(Exp,XmlDta)

        ## If length = 1 then record found so get all the paths and return the array
        if len(Tar) == 1    :
            Exp     =   reCompile('<path>(.*?)</path>',reDotAll)
            Tar     =   reFindall(Exp,Tar[0])            

            ## Ok now get all of the Emualtor XBE's.
            for Path in Tar     :
                if not ( Path.endswith('\\') )          :   Path += '\\'
                for SubDir in os.listdir( Path )        :
                    if os.path.isdir(Path + SubDir)     :
                        ## Check for Default.xbe
                        if os.path.isfile(Path + SubDir +'\\default.xbe')   :
                            print 'Will Add : '+ str(SubDir)
                            ## Check for ThumbNail
                            ImgList =   [Path + SubDir +'\\default.tbn',        \
                                         Path + SubDir +'\\default.jpg',        \
                                         Path + '\\'+ SubDir +'.jpg',           \
                                         Path + '\\'+ SubDir +'.tbn'            ]
                            for Img in ImgList  :
                                if os.path.isfile(Img)  :
                                    print 'Image Path : '+ str(Img)
                            ## Check for Rom Directory.
                            if os.path.isdir( Path + SubDir +'\\roms' )         :
                                print 'Rom Dir : '+ Path + SubDir +'\\roms\\'

        else                :
            return []
    except :
        traceback.print_exc()

g = GetEmus()
